//
//  UpLabelVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/30.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "UpLabelVC.h"
#import "IWTextView.h"

#define biaoQianTag         572

@interface UpLabelVC ()<UITextViewDelegate>
{
    NSString *biaoQian;
}
@property (weak, nonatomic) IBOutlet IWTextView *textV;

@end

@implementation UpLabelVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNav];
    [self loadUI];
    biaoQian = @"";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewChange:) name:UITextViewTextDidChangeNotification object:nil];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setNav
{
//    self.title = @"标签";
    self.title = NSLocalizedString(@"Mark", nil);
    UIButton *right = [UIButton buttonWithType:UIButtonTypeCustom];
    right.frame = CGRectMake(0, 0, 50, 50);
//    [right setTitle:@"确定" forState:UIControlStateNormal];
    [right setTitle:NSLocalizedString(@"Confirm", nil) forState:UIControlStateNormal];
    [right setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
    [right addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:right];
}

- (void)loadUI
{
    _textV.tag = biaoQianTag;
    _textV.delegate = self;
    _textV.returnKeyType = UIReturnKeyDone;
//    _textV.placeholder = @"标签：最多输入5个，用空格隔开";
    NSString *markString = NSLocalizedString(@"Mark", nil);
    NSString *pholderString = NSLocalizedString(@"Add 5 or less, Separated by Space", nil);
    _textV.placeholder = [NSString stringWithFormat:@"%@:%@",markString,pholderString];
}

- (void)textViewChange:(NSNotification *)not
{
    UITextView *textView = not.object;
    if ([textView isMemberOfClass:[IWTextView class]])
    {
        if (textView.tag == biaoQianTag)
        {
            NSArray *arr = [textView.text componentsSeparatedByString:@" "];
            if (arr.count>5)
            {
                NSMutableArray *c = [NSMutableArray arrayWithArray:arr];
                [c removeObjectsInRange:NSMakeRange(5, arr.count-5)];
                textView.text = [c componentsJoinedByString:@" "];
            }
            biaoQian = textView.text;
        }
    }
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        [self rightBtnClick];
        return NO;
    }
    return YES;
}

- (void)rightBtnClick
{
    self.sureBlock(biaoQian);
    [self.navigationController popViewControllerAnimated:YES];
}


@end
